<?php
namespace WXAPI\Controller;


use Think\Controller;
use Think\Log;

include_once 'Pay/DefaultIcbcClient.php';
include_once 'Pay/IcbcConstants.php';

class Pay3Controller extends Controller
{
    public function pay(){
        $notify_url = SITE_URL.'/WXAPI/pay/callback';
        $order_sn = get_order_sn();
        $request = array(
            "serviceUrl" => 'https://gw.open.icbc.com.cn/api/cardbusiness/qrcode/qrgenerate/V4',
            "method" => 'POST',
            "isNeedEncrypt" => false,
            "biz_content" => array(
                "mer_id"=>"240257020011",
                "out_trade_no"=>$order_sn,
                "order_amt"=>"100",
                "trade_date"=>date('Ymd',time()),
                "trade_time"=>date('His',time()),
                "pay_expire"=>86400,
                "notify_flag"=>1,
                "notify_url"=>$notify_url,
                "tporder_create_ip"=>get_client_ip(),
                "goods_name"=>'测试产品',
                "terminal_info"=>[
                    'device_type'=>01
                ],
            )

        );

        $client = new Pay\DefaultIcbcClient('11000000000000043721',
            'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqBCYYamTyUpcr8qeZ8l/YFdJ02hVmFyQa4la+Y0RoyrOEQeiNgjP9aN1QKNvY1YdThIx/0m2d62YvDzrU2E2NXYlY3avS7ZfmkqRz3sdN/iX97B5oejSop0nwHDQf3awWlX9PftaoE4CrB/3PkPB344Nbxq5+MIOXhBSH8JJpalTglPIVdiQCZqB0irvfQ7ioKABSL33s2D4aCnXTFZi3r8YiJZV8k1rgzteu4HyOt/OzF81Uz9b04MDfGeeuC/7j5/Uvw1z8x2s33Kv+LKaeMTXPZDcTJKF+QD4k+iuhAoRdCrvfJiLgnEttqWrcusZEBHoHHQjpBdKcbJB/nksBAgMBAAECggEAPpQhW4pJ1sHcn9IEq88BRT2fBeDB0J6C7RxTdmd+Zh36HpvN18xNi6BeGaO4bCMIWQ7oUiIns+FR7s9pxa9KOFbt95Y8KPrIFxfxzS56aLQ70q9m7+jPWiadOfJ/ox0KOxPS1PVoWFw5V3qtCwob4N0O3CiQ0KTVwt7cWb8MfR0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PE_RSA2,
            '',
            '',
            'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMpjaWjngB4E3ATh+G1DVAmQnIpiPEFAEDqRfNGAVvvH35yDetqewKi0l7OEceTMN1C6NPym3zStvSoQayjYV+eIcZERkx31KhtFu9clZKgRTyPjdKMIth/wBtPKjL/5+PYalLdomM4ONthrPgnkN4x4R0+D4+EBpXo8gNiAFsNwIDAQAB',
            '',
            '',
            '',
            '');
        $resp = $client->execute($request,'msgId','');
        echo $resp;
        $respObj = json_decode($resp,true);
        if($respObj["return_code"] == 0){ //sucess
            echo $respObj["return_msg"];
        }else{//fail
            echo $respObj["return_msg"];
        }
    }


    public function callback()
    {
        $result = I('');
        Log::record(json_encode(I('')), 'DEBUG');
    }


    function get_client_ip() {
        $ip = '';
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $ip = trim($ips[0]);
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}